package cn.neusoft.yuejian307.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import cn.neusoft.yuejian307.R;
import cn.neusoft.yuejian307.bean.User;
import cn.neusoft.yuejian307.listener.UserListener;
import cn.neusoft.yuejian307.model.UserModel;

import static cn.neusoft.yuejian307.GloableVirable.USERID;

public class ApplyActivity extends AppCompatActivity implements UserListener<User>{
    private TextView  t1,t2,t3,t4,t5;
    private EditText edt;
    private CheckBox ch;
    private Spinner spinner;
    private Intent  intent;
    private String SUM;
    private ImageView pic;
    private int id,number;
    MyTitle myTitle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       intent = getIntent();
        setContentView(R.layout.activity_apply);
        init();
    }

    private void init(){
        myTitle = (MyTitle)findViewById(R.id.tt);
        myTitle.setTitle("活动报名");
        id = Integer.parseInt(intent.getStringExtra("id"));
        pic  = (ImageView)findViewById(R.id.activity_picture);
        Picasso.with(ApplyActivity.this).load(intent.getStringExtra("5")).into(pic);
        t1 = (TextView)findViewById(R.id.name);
        t1.setText(intent.getStringExtra("1"));
        t2 = (TextView)findViewById(R.id.free);
        t2.setText(intent.getStringExtra("2"));
        t3 = (TextView)findViewById(R.id.replace);
        t3.setOnClickListener(new MyListener());
        t4 = (TextView)findViewById(R.id.sum);
        t4.setText("0");
        t5 = (TextView)findViewById(R.id.apply);
        t5.setOnClickListener(new MyListener());
        edt = (EditText)findViewById(R.id.phone);
        edt.setText(intent.getStringExtra("3"));
        ch = (CheckBox)findViewById(R.id.checkBox);
        ch.setOnClickListener(new MyListener());
        spinner = (Spinner)findViewById(R.id.spinner);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                switch (i){
                    case 0:
                        number = Integer.parseInt(intent.getStringExtra("2"));
                        SUM = (i+1)*number+"";
                        t4.setText(SUM);
                        break;
                    case 1:
                        number = Integer.parseInt(intent.getStringExtra("2"));
                        SUM = (i+1)*number+"";
                        t4.setText(SUM);
                        break;
                    case 2:
                        number = Integer.parseInt(intent.getStringExtra("2"));
                        SUM = (i+1)*number+"";
                        t4.setText(SUM);
                        break;
                    case 3:
                        number = Integer.parseInt(intent.getStringExtra("2"));
                        SUM = (i+1)*number+"";
                        t4.setText(SUM);
                        break;
                    case 4:
                        number = Integer.parseInt(intent.getStringExtra("2"));
                        SUM = (i+1)*number+"";
                        t4.setText(SUM);
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

    }

    @Override
    public void onResponse(User user) {
        if (user.getSuccess()==1){
            Toast.makeText(this,"报名成功",Toast.LENGTH_LONG).show();
            finish();
        }else {
            Toast.makeText(this,"请认真填写",Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(this,"链接服务器超时",Toast.LENGTH_LONG).show();
    }

    public class MyListener implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.replace:
                    edt.setText("");
                    t3.setFocusableInTouchMode(true);
                    t3.setCursorVisible(true);
                    break;
                case R.id.checkBox://已阅读
                    if (ch.isChecked())   ch.setTextColor(ApplyActivity.this.getResources().getColor(R.color.red));
                    else   ch.setTextColor(ApplyActivity.this.getResources().getColor(R.color.grey));
                    break;
                case R.id.apply:
                    apply();
            }
        }
    }

    private void apply(){
        String name = intent.getStringExtra("1");
    String price = intent.getStringExtra("2");


    String phone = edt.getText().toString();
    UserModel userModel = new UserModel();
        userModel.applyData(this,USERID,id,name,"1",price,phone,"11",number+"");
                }
                }
